/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package defender;

import Entities.*;
import java.util.ArrayList;
import java.util.Random;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

/**
 *
 * @author Shane
 */
public class GameplayState extends BasicGameState {

    int stateID = -1;
    Player thePlayer;
    ArrayList<Enemy> enemies = new ArrayList();
    ArrayList<Projectile> projectiles = new ArrayList();
    Enemy bob = null;

    GameplayState(int stateID) {
        this.stateID = stateID;
    }

    @Override
    public int getID() {
        return stateID;
        //another test
    }

    @Override
    public void init(GameContainer container, StateBasedGame game) throws SlickException {
        SpawnEnemies(10);
        thePlayer = new Player(100f, 200f, "data/plane.png", 1.0f, 65.0f);

    }

    @Override
    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
        g.drawString("GameState!", 700, 0);
        thePlayer.render(container, game, g);
        RenderEntities(container, game, g);
    }

    @Override
    public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {
        thePlayer.update(container, game, delta, projectiles);
        UpdateEntities(container, game, delta);
    }

    /**
     * Creates a number of enemies with random y positions and walkspeeds
     *
     * @param count The number of enemies to create. (starting from 0)
     *
     *
     */
    public void SpawnEnemies(int count) throws SlickException {
        Random rand = new Random();
        for (int i = 0; i <= count; i++) {
            float x = 850;
            float y = rand.nextFloat() * 500;
            float walkspeed = rand.nextFloat() * 3 + 2;
            Enemy tempEnemy = new Enemy(x, y, "data/plane.png", 1, walkspeed, 1, 1, 10);
            enemies.add(tempEnemy);
        }
    }

    public void RenderEntities(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
        //Render Enemies -------------------------------------------------------
        for (Enemy tempEnemy : enemies) {
            tempEnemy.render(container, game, g);
        }

        //Render Projectiles ---------------------------------------------------
        for (Projectile tempProj : projectiles) {
            tempProj.render(container, game, g);
        }
    }

    public void UpdateEntities(GameContainer container, StateBasedGame game, int delta) throws SlickException {
        //Update Enemies -------------------------------------------------------
        for (int i = 0; i < enemies.size() - 1; i++) {
            enemies.get(i).update();
            if (enemies.get(i).isOffScreen()) {
                enemies.remove(i);
            }
            if (enemies.get(i).alive == false) {
                enemies.remove(i);
            }
        }

        //Update Projectiles ---------------------------------------------------
        for (int i = 0; i < projectiles.size(); i++) {
            projectiles.get(i).update(delta);
            if(projectiles.get(i).CheckCollision(enemies)){
                projectiles.remove(i);
                break;
            }
            if (projectiles.get(i).isOffScreen()) {
                projectiles.remove(i);
            }

        }

    }
}
